package yxy.game.pm2.active.command.bottlewish;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actwishsignup;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心愿瓶;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BottleWishMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

/****************************************
 * @name CMD_Active_心愿瓶_BottleWishRegisterReward
 * @description 心愿瓶活动签到奖励领取
 * @author 梁健彬
 * @since 2022-08-22
 ***************************************/
@MessageType(proto = Command.BottleWishRegisterReward, classtype = BottleWishMessage.BottleWishRegisterRewardRequest.class)
public class CMD_Active_心愿瓶_BottleWishRegisterReward extends Command<BottleWishMessage.BottleWishRegisterRewardRequest> {

    @Override
    public void execute(MessageSource messageSource, BottleWishMessage.BottleWishRegisterRewardRequest request) throws Exception {
        String playercode = messageSource.getTag();
        int dataId = request.getDataId();
        BottleWishMessage.BottleWishRegisterRewardResponse.Builder response = BottleWishMessage.BottleWishRegisterRewardResponse.newBuilder();
        ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(playercode, false);
        try {

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            actwishsignup data = DataCenter.getData(dataId, actwishsignup.class);
            if (data == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            ArrayList<Integer> list = am.getHasRewardDays();
            if (list.contains(dataId)) {
                send(messageSource, response, Result.BottleWishRegisterCantRepeatedReward);//不能重复领取
                return;
            }
            CurrencyHandle currencyHandle = new CurrencyHandle();
            //todo 发奖励
            if (am.tryLock()) {
                list.add(dataId);
                Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                currencyHandle.addCurrency(playercode, CCReason.心愿瓶, curs);
                response.addAllDisplay(items);
                am.set();
            }

            response.setLogindays(am.getDay());//当前登录的天数
            response.addAllDrawDay(am.getHasRewardDays());//领取的天数
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            am.unlock();
        }

    }

    private void send(MessageSource messageSource, BottleWishMessage.BottleWishRegisterRewardResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
